import request_config from './config.js'
import { useUserStore } from '@/store/userStore.js'

function request(config) {
	if (config?.load) {
		uni.showLoading({
			mask: true
		})
	}
	if (!request_config.baseUrl) {
		uni.showToast({
			title: '未配置请求域名',
			icon: 'none',
			duration: 3000
		})
		return
	}
	if (!config.url) {
		uni.showToast({
			title: '未配置请求地址',
			icon: 'none',
			duration: 3000
		})
		return
	}
	if (config.url?.indexOf('/') == 0) {
		config.url = config.url.slice(1, config.url.length)
	}
	let url = request_config.baseUrl + config.url, // 请求路径
		data = {
			// token: uni.getStorageSync('token') || '',
			//  version: request_config.version,
		}
	if (config.data != undefined) Object.assign(data, config.data)
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method: config.method,
      dataType: 'json',
			header: {
				'ngrok-skip-browser-warning': 'test',
				'content-type': 'application/json',
				'version': request_config.version,
				'Access-Token': uni.getStorageSync('token') || '',
				...config.header,
			},
			data,		
			timeout: 60000,
			success: (result) => {
				if (config?.load) {
					uni.hideLoading()
				}
				const res = result.data
				//token过期
				if (res.code == 50001) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 1500)
					uni.clearStorageSync()
					useUserStore().userInfo = {}
					return
				}
				if (res.code == 500) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				}
				resolve(res)
			},
			fail: (error) => {
				if (config?.load) {
					uni.hideLoading()
				}
				console.log(error)
				reject(error)
				uni.showToast({
					title: '数据错误,请刷新重试',
					icon: 'none'
				})
			},
		})
	})
}
// 网络请求
export default {
	post(url = '', data = {}, options = {
		load: true
	}) {
		return request({
			method: 'POST',
			url,
			data,
			...options
		})
	},
	get(url = '', data = {}, options = {
		load: true
	}) {
		return request({
			method: 'GET',
			url,
			data,
			...options
		})
	},
	put(url = '', data = {}, options = {
		load: true
	}) {
		return request({
			method: 'PUT',
			url,
			data,
			...options
		})
	},
	delete(url = '', data = {}, options = {
		load: true
	}) {
		return request({
			method: 'DELETE',
			url,
			data,
			...options
		})
	},
	upload(url = '', data = {}, options = {
		load: true
	}, callback) {
		return new Promise((resolve, reject) => {
			if (!url) {
				uni.showToast({
					title: '缺少上传地址',
					icon: 'none'
				})
				return
			}
			let res_data = null
			if (options?.load) {
				uni.showLoading({
					mask: true
				})
			}
			if (url?.indexOf('/') == 0) {
				url = url.slice(1, url.length)
			}
			const loadTask = uni.uploadFile({
				url: request_config.baseUrl + url, //文件上传的地址
				filePath: data.filePath, //文件路径
				name: 'file', // 文件对应的 key 
				header: {
					"Access-Token": uni.getStorageSync('token') || '',
				},
				/* formData: {
					type: 'image',
					filekey: 'file',
					action: 'uploadImage'
				}, */
				success: (uploadFileRes) => {
					if (options?.load) {
						uni.hideLoading()
					}
					res_data = JSON.parse(uploadFileRes.data)
					if (res_data.code == 50001) {
						uni.showToast({
							title: '登录过期，请重新登录',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}, 1500)
						uni.clearStorageSync()
						useUserStore().userInfo = {}
						return
					}
					resolve(res_data)					
				},
				fail: (err) => {
					if (options?.load) {
						uni.hideLoading()
					}
					uni.showToast({
						title: '上传失败,请重新尝试',
						icon: 'none'
					})
					reject(err)
				}
			})
			loadTask.onProgressUpdate(res => {
				const progress = res.progress
				callback?.(progress)
			})

		})
	}
}